package miiteen.hb.db.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import miiteen.core.result.ResultMessage;
import miiteen.core.result.ResultMessageTool;
import miiteen.customsportal.basic.service.HistoryViewService;
import miiteen.data.beans.PageBounds;
import miiteen.hb.db.beans.DbInfo;
import miiteen.hb.db.service.DbInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("督办管理")
@RequestMapping("/hb/db")
@RestController
@CrossOrigin
public class DbInfoController {

    @Autowired
    private DbInfoService dbInfoService;

    @Autowired
    private HistoryViewService historyViewService;
    @ApiOperation("关级督办")
    @GetMapping("/gjdb")
    public ResultMessage<List<DbInfo>> gjdb(@RequestParam int pageSize, @RequestParam int pageNum, String subject) {
        //获取查询条件
        PageBounds pageBounds = new PageBounds();
        pageBounds.setPageNum(pageNum);
        pageBounds.setPageSize(pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("orderby", "CREATE_TIME desc");
        map.put("publishFlag", 1);
        if (StringUtils.isNotBlank(subject)) {
            map.put("subject", "%" + subject + "%");
        }


        //查询数据
        List<DbInfo> list = dbInfoService.findListByMap(map, pageBounds);
        historyViewService.fillVisitCount(list);
        if (list != null && list.size() > 0) {
            return ResultMessageTool.Success(String.valueOf(pageBounds.getTotal()), list);
        } else {
            return ResultMessageTool.Success("0", new ArrayList<>());
        }
    }
}
